package com.alon.spring01.common.config.thread;

import com.alon.spring01.common.config.spring.SpringUtil;
import com.alon.spring01.mapper.InsuranceBaseInfoMapper;
import com.alon.spring01.model.InsuranceBaseInfo;
import java.util.List;
import java.util.concurrent.Future;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

/**
 * @program: spring01
 * @description: 线程类
 * @author: yulongguang
 * @create: 2019-04-27 18:12
 **/

@Component
public class SyncBookHandler {

    @Autowired
    private InsuranceBaseInfoMapper infoMapper;

    private static final Logger LOG = LoggerFactory.getLogger(SyncBookHandler.class);

    /**
     * syncMargePsr:(多线程同步处理数据方法). <br/>
     *
     * @param infos 一段数据集合
     * @param pageIndex 段数
     * @return Future<String> future对象
     * @author LW
     * @since JDK 1.8
     */
    @Async
    public Future<String> syncMargePsr(List<InsuranceBaseInfo> infos, int pageIndex) {

        LOG.info(String.format("此批数据的段数为:%s 此段数据的数据条数为:%s", pageIndex, infos.size()));
        //声明future对象
        Future<String> result = new AsyncResult<String>("");
        //循环遍历该段旅客集合
        if (CollectionUtils.isNotEmpty(infos)) {

            try {
                //数据入库操作
                infoMapper.insertList(infos);

            } catch (Exception e) {

                //记录出现异常的时间，线程name
                result = new AsyncResult<String>(
                        "fail,time=" + System.currentTimeMillis() + ",thread id=" + Thread
                                .currentThread().getName() + ",pageIndex=" + pageIndex);

            }
        }
        return result;
    }


}
